﻿function equaliseHeight(elements) {
    var maxHeight = 0,
            topPosition = 0,
            rows = [];
    var i;

    elements.each(function () {

        var currentItem = $(this);

        currentItem.height('auto');

        if (topPosition !== currentItem.offset().top) {
            // new row

            for (i = 0; i < rows.length; i++) {
                rows[i].height(maxHeight);
            }

            rows.length = 0;
            topPosition = currentItem.offset().top;
            maxHeight = currentItem.height();
            rows.push(currentItem);

        } else {
            // current row

            rows.push(currentItem);

            if (currentItem.height() > maxHeight) {
                maxHeight = currentItem.height();
            }
        }

    });

    // handle remainder
    for (i = 0; i < rows.length; i++) {
        rows[i].height(maxHeight);
    }
}

function resizeItems() {
    equaliseHeight($(".products-tiled").find(".product-wrapper h3"));
    equaliseHeight($(".products-tiled").find(".product-wrapper a.product"));
    equaliseHeight($(".news").find(".news-section-wrapper .news-container"));
    equaliseHeight($(".link-wrapper").find("a"));
    equaliseHeight($(".item-wrapper").find(".item .stamp-group"));
    equaliseHeight($(".item-wrapper").find(".item"));
}

var windowResizeTimeoutId;

$(document).ready(function () {
    resizeItems();

    $(window).resize(function () {
        clearTimeout(windowResizeTimeoutId);
        windowResizeTimeoutId = setTimeout(resizeItems(), 100);
    });

    $("#tab1Products").click(function () {
        resizeItems();
    });
    $("#tab2Products").click(function () {
        resizeItems();
    });
    $("#tab3Products").click(function () {
        resizeItems();
    });
    $("#btnBuyNowPromote").click(function () {
        $("#promotionLightbox").show();
        return false;
    });
    $("#btnQuick").click(function () {
        $("#lightboxPopup").show();
        return false;
    });
    $("#btnBuyNowPromote2").click(function () {
        $("#promotionLightbox").show();
        return false;
    });
    $("#btnBuyNowPromote3").click(function () {
        $("#promotionLightbox").show();
        return false;
    });


    $(".btnSS").click(function () {
        var href = $(this).attr('href');
        $("#imgSS").attr('src', href);
        $("#imageLightbox").show();
        return false;
    });

    $("#btnTestimonials").click(function () {
        $("#lightboxTestimonials").show();
        return false;
    });

    $("#btnFAQs").click(function () {
        $("#lightboxFAQs").show();
        return false;
    });

    $(".closeImageLB").click(function () {
        $("#imageLightbox").hide();
        return false;
    });

    $(".btnClosePopup").click(function () {
        $("#lightboxPopup").hide();
        return false;
    });

    $(".btnCloseFAQs").click(function () {
        $("#lightboxFAQs").hide();
        return false;
    });

    $(".btnCloseTestimonials").click(function () {
        $("#lightboxTestimonials").hide();
        return false;
    });


});